﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace V5_EaspLibs.Core {
    /// <summary>
    /// 视频转换类
    /// </summary>
    public class ffmpegHelper {
        private static readonly ffmpegHelper m_Instance = new ffmpegHelper();
        /// <summary>
        /// 
        /// </summary>
        public static ffmpegHelper Instance {
            get { return m_Instance; }
        }
        string ffmpegPath = "tools/ffmpeg.exe";
        /// <summary>
        /// ffmpeg.exe 路径
        /// </summary>
        public string FfmpegPath {
            get { return ffmpegPath; }
            set { ffmpegPath = value; }
        }
        /// <summary>
        /// 视频格式转为Flv
        /// </summary>
        /// <param name="vFileName">原视频文件地址</param>
        /// <param name="ExportName">生成后的FLV文件地址</param>
        public bool ConvertFlv(string vFileName, string ExportName) {
            string ffmpeg = HttpContext.Current.Server.MapPath("~/") + FfmpegPath;
            if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) {
                return false;
            }
            vFileName = HttpContext.Current.Server.MapPath(vFileName);
            ExportName = HttpContext.Current.Server.MapPath(ExportName);
            string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s  480*360 \"" + ExportName + "\""; //Flv格式     
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = ffmpeg;
            p.StartInfo.Arguments = Command;
            p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/tools/");
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = false;
            //开始执行   
            p.Start();
            p.BeginErrorReadLine();
            p.WaitForExit();
            p.Close();
            p.Dispose();
            return true;
        }
        /// <summary>
        /// 生成FLV视频的缩略图
        /// </summary>
        /// <param name="vFileName">视频文件地址</param>
        /// <returns></returns>
        public string CatchImg(string vFileName) {
            string ffmpeg = HttpContext.Current.Server.MapPath("~/") + FfmpegPath;
            if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName))))
                return "";
            try {
                string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + ".jpg";
                string FlvImgSize = "240x180";
                string Command = " -i " + HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + FlvImgSize + " " + HttpContext.Current.Server.MapPath(flv_img_p);
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = ffmpeg;
                p.StartInfo.Arguments = Command;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                try {
                    p.Start();
                }
                catch {
                    return "";
                }
                finally {
                    p.Close();
                    p.Dispose();
                }
                System.Threading.Thread.Sleep(4000);
                //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p))) {
                    return flv_img_p;
                }
                return "";
            }
            catch {
                return "";
            }
        }
    }
}
